/* @(#)GSOperatorDAO.java    
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-9   新增 ��b
 *   Jenny Li     2006-3-8   添加方法isExist(String,Connection),queryOper(GSOperatorBean,Connection)
 *                             modifyPwd(Connection,String,String)
 *   Jenny Li     2006-3-10  添加方法addOperator,modOperator,delOperator,queryOperBySql 
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.dao;

import java.sql.Connection;
import java.util.ArrayList;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.exception.GSException;

/**
 * 操作员表操作类����Ա�������
 *
 */
public class GSOperatorDAO extends GSBaseDAO {
    /**
     * 判断操作员是否存在
     * @param operId            操作员编码
     * @return                  存在返回true，不存在返回false
     * @throws GSException
     */
    public boolean isExist(String operId) throws GSException{
        String tSql = "select * from GS_Operator where GS_OPER_ID = ?";
        boolean tFlag = false;
        Object[] tObj = {operId};
        try{
            ArrayList tList = this.executeQuery(new GSOperatorBean(),tSql,tObj);
            if(tList.size() > 0){
                tFlag = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tFlag;
    }
    /**
     * 判断操作员是否存在
     * @param tOperId       操作员编码
     * @param aConn         DB连接
     * @return
     * @throws GSException
     */
    public boolean isExist(String aOperId,Connection aConn) throws GSException{
        String tSql = "select * from GS_Operator where GS_OPER_ID = ? and GS_OPER_VALIDITY='0'";
        boolean tFlag = false;
        Object[] tObj = {aOperId};
        try{
            ArrayList tList = this.executeQuery(aConn,new GSOperatorBean(),tSql,tObj);
            if(tList.size() > 0){
                tFlag = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tFlag;
    }
    /**
     * 查询操作员详细信息
     * @param aGSOperatorBean       操作员Bean对象
     * @return              
     * @throws GSException
     */
    public GSOperatorBean queryOperById(String operId) throws GSException{
        String tSql = "select * from GS_Operator where GS_OPER_ID = ? ";
        ArrayList tList = null;
        GSOperatorBean tGSOperatorBean = null;
        Object[] tObj = {operId};
        try{
            tList = this.executeQuery(new GSOperatorBean(),tSql,tObj);
            if(tList.size() == 1){
                tGSOperatorBean = (GSOperatorBean)tList.get(0);
            }else{
                throw new GSException(GSException.E10002,GSException.E10002_MSG);
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tGSOperatorBean;
    }

    /**
     * 查询操作员详细信息
     * @param aGSOperatorBean       操作员Bean对象
     * @return              
     * @throws GSException
     */
    public GSOperatorBean queryOper(GSOperatorBean aGSOperatorBean) throws GSException{
        String tSql = "select * from GS_Operator where GS_OPER_ID = ? and GS_OPER_PWD=? and GS_OPER_VALIDITY=?";
        ArrayList tList = null;
        GSOperatorBean tGSOperatorBean = null;
        Object[] tObj = {aGSOperatorBean.getGs_oper_id(),aGSOperatorBean.getGs_oper_pwd(),aGSOperatorBean.getGs_oper_validity()};
        try{
            tList = this.executeQuery(new GSOperatorBean(),tSql,tObj);
            if(tList.size() == 1){
                tGSOperatorBean = (GSOperatorBean)tList.get(0);
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tGSOperatorBean;
    }
    /**
     * 查询操作员信息
     * @param aGSOperatorBean       操作员Bean对象
     * @param aConn                 DB连接
     * @return
     * @throws GSException
     */
    public GSOperatorBean queryOper(GSOperatorBean aGSOperatorBean,Connection aConn) throws GSException{
        String tSql = "select * from GS_Operator where GS_OPER_ID = ? and GS_OPER_PWD=?";
        ArrayList tList = null;
        GSOperatorBean tGSOperatorBean = null;
        Object[] tObj = {aGSOperatorBean.getGs_oper_id(),aGSOperatorBean.getGs_oper_pwd()};
        try{
            tList = this.executeQuery(aConn,new GSOperatorBean(),tSql,tObj);
            if(tList.size() == 1){
                tGSOperatorBean = (GSOperatorBean)tList.get(0);
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tGSOperatorBean;
    }

    /**
     * 根据条件组成不同SQL查询操作员信息
     * @param aGSOperatorBean
     * @return
     * @throws GSException
     */
    public ArrayList queryOperByOpt(GSOperatorBean aGSOperatorBean) throws GSException{
        ArrayList tList = null;
        StringBuffer tSql = new StringBuffer("select * from GS_Operator ");
        GSOperatorBean tGSOperatorBean = new GSOperatorBean();
        Object[] tObj = {};
        if(aGSOperatorBean.getGs_oper_org_id().equals("0000")){
            tSql.append("where GS_OPER_ORG_ID like '%' ");
        }else{
            tSql.append("where GS_OPER_ORG_ID like '%"+aGSOperatorBean.getGs_oper_org_id() + "%' ");
        }
        if(aGSOperatorBean.getGs_oper_id()!=null){
            tSql.append("and GS_OPER_ID like '%" + aGSOperatorBean.getGs_oper_id() + "%' ");
        }
        if(aGSOperatorBean.getGs_oper_name() != null){
            tSql.append("and GS_OPER_NAME like '%" + aGSOperatorBean.getGs_oper_name() + "%' ");
        }
        GSLogWriter.debug(this,tSql.toString());
        try{
            tList = this.executeQuery(tGSOperatorBean,tSql.toString(),tObj);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    /**
     * 判断是否是录入人员
     * @param aOperID 操作员id
     * @param aType 类型：1-运营；2-巴士
     * @return
     * @throws GSException
     */
    public boolean isRecorder(String aOperID,String aType) 
    	throws GSException{
    	if(aType == null || aType.equals("")) return false;
    	String tSql = "";
    	if(aType.equals("1")) tSql= "select count(*) from GS_OPER_FUNCTION where GS_OPER_ID = ? and GS_FUNC_ID = '1001' and GS_OPER_VALIDITY = '0'";
    	else if(aType.equals("2")) tSql= "select count(*) from GS_OPER_FUNCTION where GS_OPER_ID = ? and GS_FUNC_ID = '1501' and GS_OPER_VALIDITY = '0'";
    	else return false;
    	
    	boolean tResult = false;
        Object[] tObj = {aOperID};
        try{
            int tCount = this.queryRowNum(tSql,tObj);
            if(tCount > 0){
            	tResult = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        
    	return tResult;
    }
    
    /**
     * 判断是否是初审人员
     * @param aOperID 操作员id
     * @param aType 类型：1-运营；2-巴士
     * @return
     * @throws GSException
     */
    public boolean isFirstAuditor(String aOperID,String aType) 
    	throws GSException{
    	if(aType == null || aType.equals("")) return false;
    	String tSql = "";
    	if(aType.equals("1")) tSql= "select count(*) from GS_OPER_FUNCTION where GS_OPER_ID = ? and GS_FUNC_ID = '1002' and GS_OPER_VALIDITY = '0'";
    	else if(aType.equals("2")) tSql= "select count(*) from GS_OPER_FUNCTION where GS_OPER_ID = ? and GS_FUNC_ID = '1502' and GS_OPER_VALIDITY = '0'";
    	else return false;
    	
    	boolean tResult = false;
        Object[] tObj = {aOperID};
        try{
            int tCount = this.queryRowNum(tSql,tObj);
            if(tCount > 0){
            	tResult = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        
    	return tResult;
    }
    
    /**
     * 判断是否是复审人员
     * @param aOperID 操作员id
     * @return
     * @throws GSException
     */
    public boolean isReviewer(String aOperID,String aType) 
    	throws GSException{
    	if(aType == null || aType.equals("")) return false;
    	String tSql = "";
    	if(aType.equals("1")) tSql= "select count(*) from GS_OPER_FUNCTION where GS_OPER_ID = ? and GS_FUNC_ID = '1003' and GS_OPER_VALIDITY = '0'";
    	else if(aType.equals("2")) tSql= "select count(*) from GS_OPER_FUNCTION where GS_OPER_ID = ? and GS_FUNC_ID = '1503' and GS_OPER_VALIDITY = '0'";
    	else return false;
    	boolean tResult = false;
        Object[] tObj = {aOperID};
        try{
            int tCount = this.queryRowNum(tSql,tObj);
            if(tCount > 0){
            	tResult = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        
    	return tResult;
    }
    /**
     * 查询操作员
     * @param aSql      查询SQL
     * @return
     * @throws GSException
     */
    public ArrayList queryOperBySql(String aSql) throws GSException{
        ArrayList tList = new ArrayList();
        try{
            tList = this.executeQuery(new GSOperatorBean(),aSql,new Object[]{});
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;

    }
    /**
     * 修改操作员密码
     * @param aConn             DB连接
     * @param aOperId           操作员编码
     * @param aNewPwd           新密码
     * @return
     * @throws GSException
     */
    public boolean modifyPwd(Connection aConn,String aOperId,String aNewPwd)throws GSException{
        boolean tFlag = false;
        String tSql = "update GS_OPERATOR set GS_OPER_PWD=? where GS_OPER_ID=?";
        Object[] tObj = {aNewPwd, aOperId};
        try{
            tFlag = this.executeUpdate(aConn,tSql,tObj);
            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }
        return tFlag;
    }
    /**
     * 新增操作员
     * @param aOperBean     操作员信息封装Bean对象
     * @return
     * @throws GSException
     */
    public boolean addOperator(GSOperatorBean aOperBean) throws GSException{
        boolean tFlag = false;
        String tSql = "insert into GS_OPERATOR (GS_OPER_ORG_ID,GS_OPER_ID,GS_OPER_NAME,GS_OPER_PWD,GS_OPER_POSITION," +
                "GS_OPER_SEX,GS_OPER_PHONE,GS_OPER_EMAIL,GS_OPER_VALIDITY,GS_LAST_MODIFY_DT)values(?,?,?,?,?,?,?,?,?,?)";
        
        Object[] tObj = new Object[10]; 
        tObj[0] = aOperBean.getGs_oper_org_id();
        tObj[1] = aOperBean.getGs_oper_id();
        tObj[2] = aOperBean.getGs_oper_name();
        tObj[3] = aOperBean.getGs_oper_pwd();
        tObj[4] = aOperBean.getGs_oper_position();
        tObj[5] = aOperBean.getGs_oper_sex();
        tObj[6] = aOperBean.getGs_oper_phone();
        tObj[7] = aOperBean.getGs_oper_email();
        tObj[8] = aOperBean.getGs_oper_validity();
        tObj[9] = aOperBean.getGs_last_modify_dt();
        try{
            tFlag = this.executeUpdate(tSql,tObj);
            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }
        return tFlag;
        
    }
    /**
     * 修改操作员信息
     * @param aOperBean     操作员Bean对象
     * @return
     * @throws GSException
     */
    public boolean modOperator(GSOperatorBean aOperBean) throws GSException{
        boolean tFlag = false;
        String tSql = "update GS_OPERATOR set GS_OPER_ORG_ID=?,GS_OPER_NAME=?,GS_OPER_POSITION=?," +
                "GS_OPER_SEX=?,GS_OPER_PHONE=?,GS_OPER_EMAIL=?,GS_OPER_VALIDITY=?,GS_LAST_MODIFY_DT=? where GS_OPER_ID=?";
        
        Object[] tObj = new Object[9]; 
        tObj[0] = aOperBean.getGs_oper_org_id();
        tObj[1] = aOperBean.getGs_oper_name();
        tObj[2] = aOperBean.getGs_oper_position();
        tObj[3] = aOperBean.getGs_oper_sex();
        tObj[4] = aOperBean.getGs_oper_phone();
        tObj[5] = aOperBean.getGs_oper_email();
        tObj[6] = aOperBean.getGs_oper_validity();
        tObj[7] = aOperBean.getGs_last_modify_dt();
        tObj[8] = aOperBean.getGs_oper_id();
        try{
            tFlag = this.executeUpdate(tSql,tObj);
            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }
        return tFlag;
        
    }
    /**
     * 删除操作员
     * @param aOperId       操作员编码
     * @return
     * @throws GSException
     */
    public boolean delOperator(String aOperId) throws GSException{
        boolean tFlag = false;
        String tSql = "delete from GS_OPERATOR where GS_OPER_ID=?";
        Object[] tObj = {aOperId};
        try{
            tFlag = this.executeUpdate(tSql,tObj);
            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }
        return tFlag;
    }
    
    /**
     * 用于超级管理员直接修改操作员密码的方法
     * @param tConn
     * @param operId
     * @param newpwd
     * @param tCurDT
     * @return
     * @throws GSException
     */
    public boolean modOperatorPwd(Connection tConn,String operId,String newpwd,String tCurDT) throws GSException {
        boolean tFlag = false;
        String tSql = "update GS_OPERATOR set gs_oper_pwd = ?,gs_last_modify_dt = ? where GS_OPER_ID=?";
        Object[] tObj = {newpwd,tCurDT,operId};
        try{
            tFlag = this.executeUpdate(tConn,tSql,tObj);
            
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19995,GSException.E19995_MSG);
        }
        return tFlag;       
    }
}
